package com.jcw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcw.common.R;
import com.jcw.domain.Employee;
import com.jcw.service.EmployeeService;
import com.jcw.mapper.EmployeeMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
* @author JCW
* @description 针对表【employee(员工信息)】的数据库操作Service实现
* @createDate 2023-03-27 17:12:44
*/
@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{

    @Resource
    private HttpSession session;

    @Override
    public R<Employee> login(Employee e) {
        //根据name查询数据
        Employee employee = query().eq("username", e.getUsername()).one();
        //如果name存在，再查询出数据库中关于这个名字的所有字段
        if (BeanUtil.isEmpty(employee)){
            return R.error("用户不存在");
        }
        //将前端传来的明文密码用md5加密
        String md5Hex = DigestUtil.md5Hex(e.getPassword());
        //判断数据库中保存的密码是否和前端传来的密码一致
        if (!employee.getPassword().equals(md5Hex)){
            return R.error("输入的账号或密码有误");
        }
        //如果密码也没有问题，再判断账号是否被禁用
        if (employee.getStatus()==0) {
            return R.error("账户被禁用，登录失败");
        }
        //全部都没有问题，将用户的id保存在session当中
        session.setAttribute("employee",employee.getId());
        log.info("用户登录成功");
        return R.success(employee);
    }

    @Override
    public R<String> logout() {
        //销毁session中的用户数据
        session.removeAttribute("employee");
        log.info("用户退出成功");
        return R.success("退出成功");
    }

    @Override
    public R<String> add(Employee employee) {
        employee.setPassword(DigestUtil.md5Hex("123456"));

        //添加员工
        boolean save = save(employee);
        if (save){
            log.info("添加员工成功");
            return R.success("添加成功");
        }
        log.info("添加员工失败");
        return R.error("添加失败");
    }

    @Override
    public R<Page> pagination(int page, int pageSize, String name) {
        //创建分页构造器
        Page pageInfo = new Page(page,pageSize);
        //根据名称查询、分页之后返回
        query().like(StrUtil.isNotEmpty(name), "name", name)
                .orderByDesc("update_time")
                .page(pageInfo);
        return R.success(pageInfo);
    }

    @Override
    public R<Employee> getEmployee(Long id) {
        //根据id查询员工信息
        Employee employee = getById(id);
        if (BeanUtil.isEmpty(employee)){
            return R.error("发生错误，员工信息为空");
        }
        //返回员工信息用于前端回显数据
        return R.success(employee);
    }

    @Override
    public R<String> editEmployee(Employee employee) {
        //根据员工id修改员工信息
        if (updateById(employee)) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
}




